<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="banner in SwiperData" :key="banner.id">
        <img :src="banner.imgUrl" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
//引入轮播图
import Swiper from "swiper";
//引入轮播图样式
import "swiper/css/swiper.min.css";

export default {
  name: "Swiper",
  props:['SwiperData'],
  //因为页面打包后才会请求数据，所以可能刚开始轮播图无法使用，没有数据，使用$nextTik
  //wantch监视bannerData的变化,等待数据和页面同步后才开始使用new swiper
  watch: {
    SwiperData: {
      //公共组件有的需要立即刷新有的不需要可以都加上
      immediate: true,
      handler() {
        this.$nextTick(() => {
          //加了immediate，会在数据没有时也会刷新，会有滑动问题
          //判断数据是否为空
          if(!this.SwiperData.length) return
          new Swiper(".swiper-container", {
            direction: "horizontal", // 垂直切换选项
            loop: true, // 循环模式选项

            // 如果需要分页器
            pagination: {
              el: ".swiper-pagination",
            },

            // 如果需要前进后退按钮
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          });
        });
      },
    },
  },
};
</script>

<style>
</style>